package com.ambimmort.shj.service.system.property;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Systemproperty;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Josh on 2015/7/31.
 */
@Service
public class SystemPropertyService {
//    @Autowired
//    private SessionFactory hibernateSessionFactory;
    ResultSet rs =null;
    BaseDao baseDao = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }

    public String getListProperty(String property){
        JSONArray jsonArr = JSONArray.fromObject(property);
        JSONArray returnJsonArr = new JSONArray();
        init();
            try {
                JSONObject jo;
                for (int i = 0; i < jsonArr.size(); i++) {
                Object[] p ={jsonArr.getString(i)};
                rs=baseDao.executeSQL("select systemprop0_.property as property50_0_, systemprop0_.value as value50_0_ from shj_db.systemproperty systemprop0_ where systemprop0_.property=?",p);
//                    rs=baseDao.executeSQL("select value from systemproperty  where property=?",p);
                jo = new JSONObject();
                jo.put(jsonArr.getString(i), rs.getString("value"));
                 returnJsonArr.add(jo);
                }
                return returnJsonArr.toString();
            } catch (SQLException e) {
                e.printStackTrace();
                return "[]";
            }finally {
                close();
            }
//        JSONArray jsonArr = JSONArray.fromObject(property);
//        JSONArray returnJsonArr = new JSONArray();  //return jsonArr
//        SessionFactory sf = HibernateUtil.getSessionFactory();
//        Session session = sf.openSession();
//        session.clear();
//        try {
//            Systemproperty systemProperty;
//            JSONObject jo;
//            for (int i = 0; i < jsonArr.size(); i++) {
//                systemProperty = (Systemproperty) session.get(Systemproperty.class, jsonArr.getString(i));
//                jo = new JSONObject();
//                jo.put(jsonArr.getString(i), systemProperty.getValue());
//                returnJsonArr.add(jo);
//            }
//            return returnJsonArr.toString();
//        }catch (Exception e) {
//            e.printStackTrace();
//            return "[]";
//        } finally {
//            session.close();
//            sf.close();
//        }
    }

    public String getProperty(String property){
        init();
        String sql = "select value from systemproperty where property=?";
        JSONObject obj = new JSONObject();
        JSONArray jSONArray = new JSONArray();
        try {
            Object[] p = {property};
            rs =baseDao.executeSQL(sql, p);
            obj.put("value",rs.getString("value"));
            jSONArray.add(obj);
            return jSONArray.toString();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            close();
        }
//        SessionFactory sf = HibernateUtil.getSessionFactory();
//        Session session = sf.openSession();
//        session.clear();
//        try {
//            Systemproperty systemProperty = (Systemproperty) session.get(Systemproperty.class, property);
//            String value = systemProperty.getValue();
//            return value;
//        }
//        finally {
//            session.close();
//            sf.close();
//        }
    }

    public String setListProperty(String property) {
        init();
        String sql = "select * from systemproperty";
        JSONArray jsonArr = JSONArray.fromObject(property);
        JSONObject jo = null;
        Systemproperty systemProperty =null;
        try {
            for (int i = 0; i < jsonArr.size(); i++) {
                jo = jsonArr.getJSONObject(i);
                for (Object key : jo.keySet()) {
                    Object[] p ={key.toString()};
                    rs=baseDao.executeSQL(sql,p);
                    Object[] param={jo.getString(key.toString())};
                    baseDao.executeUpdate("update systemproperty set value=? where property=?",param);
                }
            }
            return "{\"result\":\"true\"}";
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"result\":\"false\"}";
        }
        finally {
            close();
        }


//        SessionFactory sf = HibernateUtil.getSessionFactory();
//        Session session = sf.openSession();
//        session.clear();
//        Transaction tx = session.beginTransaction();
//        JSONArray jsonArr = JSONArray.fromObject(property);
//        JSONObject jo = null;
//        Systemproperty systemProperty =null;
//            try {
//            for (int i = 0; i < jsonArr.size(); i++) {
//                jo = jsonArr.getJSONObject(i);
//                for (Object key : jo.keySet()) {
//                    systemProperty = (Systemproperty) session.get(Systemproperty.class, key.toString());
//                    systemProperty.setValue(jo.getString(key.toString()));
//                    session.update(systemProperty);
//                }
//            }
//            tx.commit();
//            return "{\"result\":\"true\"}";
//        } catch (Exception e) {
//            e.printStackTrace();
//            tx.rollback();
//            return "{\"result\":\"false\"}";
//        }
//        finally {
//            session.close();
//        }

    }

    public String setProperty(String property,String value){
        init();
        String sql = "select * from systemproperty where property=?";
        Object[] p = {property};
        rs = baseDao.executeSQL(sql, p);

        try {
            if (rs.next()) {
                try {
                    Object[] param = {value, property};
                    sql = "update systemproperty set value=? where property=?";
                    baseDao.executeUpdate(sql, param);
                    return "{\"result\":\"true\"}";
                } catch (Exception e) {
                    return "{\"result\":\"false\"}";

                }
            }
            else {
                return "{\"result\":\"false\"}";
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return "{\"result\":\"false\"}";
        }finally {
            close();
        }
//        SessionFactory sf = HibernateUtil.getSessionFactory();
//        Session session = sf.openSession();
//        session.clear();
//        Systemproperty systemProperty = (Systemproperty) session.get(Systemproperty.class, property);
//        if (systemProperty != null) {
//            Transaction tx = session.beginTransaction();
//            try {
//                systemProperty.setValue(value);
//                session.update(systemProperty);
//                tx.commit();
//                return "{\"result\":\"true\"}";
//            } catch (Exception e) {
//                tx.rollback();
//                return "{\"result\":\"false\"}";
//            } finally {
//                session.close();
//            }
//        } else {
//            try {
//                return "{\"result\":\"false\"}";
//            }
//            finally {
//                session.close();
//            }
//        }
//
    }
}
